/**********************************
 * Assignment 5: Alien vs Predator
 * Date: 12/04/2011
 *
 * Alexander Miller: ahm82, 2605797
 * David Felty: djf242, 2828357
 * David Hau: dch229, 2586217
 * Hanna Atmer: ha237, 2666426
 **********************************/

package avp;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Color;

public class GUI extends JFrame {

	private static final BasicStroke NORMAL = new BasicStroke(1);
	private static final BasicStroke BOLDED = new BasicStroke(3);
	private static final BasicStroke DASHED = new BasicStroke(
			1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10, new float[] {1, 3}, 0);
	
	private static final Color ALIEN_COLOR    = Color.GREEN;
	private static final Color PREDATOR_COLOR = Color.RED;
	private static final Color CONTROL_COLOR  = Color.WHITE;
	private static final Color SCANNER_COLOR  = Color.ORANGE;
	
	private static final Color CLOSED_COLOR = Color.RED;
	private static final Color BURNED_COLOR = Color.ORANGE;
	private static final Color WALL_COLOR   = Color.BLACK;
	
	private static final Color BACKGROUND_COLOR = new Color(0x6699FF);
	
	private final Ship s;
	private JPanel gScreen = new ShipPanel();
	private ColorLabel log = new ColorLabel("Fight!", Color.BLACK);
	private JPanel infoBar = new JPanel();

	public GUI(Ship ship) {
		s = ship;
		// adds all the pieces
		infoBar.add(new ColorLabel("Alien", ALIEN_COLOR));
		infoBar.add(new ColorLabel("Predator", PREDATOR_COLOR));
		infoBar.add(new ColorLabel("Scanner", SCANNER_COLOR));
		infoBar.add(new ColorLabel("Control Room", CONTROL_COLOR));
		infoBar.setBackground(Color.BLACK);
		gScreen.setBackground(BACKGROUND_COLOR);
		log.label.setForeground(Color.WHITE);
		
		add(infoBar, BorderLayout.NORTH);
		add(gScreen, BorderLayout.CENTER);
		add(log, BorderLayout.SOUTH);
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(20 * GameConstants.WIDTH, 20 * GameConstants.HEIGHT);
	}
	
	public void setMessage(String text) {
		log.label.setText(text);
	}
	
	private static final class ColorLabel extends JPanel {
		JLabel label;
		ColorLabel(String text, Color bgColor) {
			label = new JLabel(text);
			add(label);
			setBackground(bgColor);
		}
	}

	private final class ShipPanel extends JPanel {
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2d = (Graphics2D) g;
			int sideLength = Math.min(getWidth()/(s.width+1), getHeight()/(s.height+1));
			int xMargin = (getWidth()  - sideLength*s.width)  / 2;
			int yMargin = (getHeight() - sideLength*s.height) / 2;
			int xCor = xMargin;
			int yCor = yMargin;
			int xNext = xCor + sideLength;
			int yNext = yCor + sideLength;

			for (Node[] row : s.grid) {
				for (Node n : row) {
					drawEdge(n.get(Dir.NORTH),g2d, xCor, yCor, xNext, yCor);
					drawEdge(n.get(Dir.WEST), g2d, xCor, yCor, xCor, yNext);
					
					// Bottom edge
					if (n.row == s.grid.length-1)
						drawEdge(n.get(Dir.SOUTH), g2d, xCor, yNext, xNext, yNext);
					// Right edge
					if (n.col == row.length-1)
						drawEdge(n.get(Dir.EAST), g2d, xNext, yCor, xNext, yNext);

					xCor = xNext;
					xNext += sideLength;
					// Draw circle agent, control room, or scanner
					g.setColor(getBackground());
					if (n == s.alienNode) {
						g.setColor(ALIEN_COLOR);
					} else if (n == s.predatorNode) {
						g.setColor(PREDATOR_COLOR);
					} else if (n == s.controlNode) {
						g.setColor(CONTROL_COLOR);
					} else if (n == s.scannerNode) {
						g.setColor(SCANNER_COLOR);
					}

					if (g.getColor() != getBackground()) {
						g.fillOval(xCor - sideLength, yCor, sideLength, sideLength);
					}

				}// end for
				
				xCor = xMargin;
				yCor = yNext;
				xNext = xCor + sideLength;
				yNext += sideLength;
			}// end outer for
			
		};// end paint
	};// end gScreen declaration
	
	private static void drawEdge(Edge e, Graphics2D g, int x1, int y1, int x2, int y2) {
		EdgeState es = e.state;
		if (es == null) {
			g.setColor(WALL_COLOR);
			g.setStroke(BOLDED);
		} else {
			switch (es) {
			case OPEN:
				return;
			case CLOSED:
				g.setColor(CLOSED_COLOR);
				g.setStroke(NORMAL);
				break;
			case BURNED:
				g.setColor(BURNED_COLOR);
				g.setStroke(DASHED);
				break;
			}// end switch
		}
		g.drawLine(x1, y1, x2, y2);
	}

}// end class
